package com.younion.action.consumer.impl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IRegisterUserService;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.vo.RegisterUserVO;

public class IphoneDeviceAddConsumer extends AbstractRequestConsumer {
	private IRegisterUserService registerUserService = null;
	
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return addDevice(request, response);
	}

	public String addDevice(HttpServletRequest request,
			HttpServletResponse response) {
		String userToken = request.getParameter("user_token");
		String deviceToken = request.getParameter("device_token");
		Map paramMap = new HashMap();
		paramMap.put("userToken", userToken);
		ResultObject resultObject = new ResultObject();
		try {
			RegisterUserVO registerUserVO = registerUserService.getVOByMap(paramMap);
			if(registerUserVO!=null){
				registerUserVO.setDeviceToken(deviceToken);
				registerUserService.save(registerUserVO);
				resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
			}else{
				resultObject.setStatus(ApiConstant.FALSE_STATUS);
				resultObject.setMsg("不存在此用户");
			}
		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public void setRegisterUserService(IRegisterUserService registerUserService) {
		this.registerUserService = registerUserService;
	}
}
